// 创建数据库连接
let mysql = require('mysql')
let connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '123',
    database: 'school'
})
connection.connect(function(err){
    if(err){
        console.log(err)
    }
    console.log('连接数据库成功')
})
// 创建express
let express = require('express')
// 路由
let app = express()
// 设置允许跨域访问
//设置跨域访问
app.all("*",function(req,res,next){
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin","*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers","content-type");
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200);  //让options尝试请求快速结束
    else
        next();
})
// 查询
app.get('/hello',function(resw,res){
    let sql = 'select * from student'
    connection.query(sql,function(error,results,filed){
        if(error){
            throw error
        }
        res.send(results)
        console.log(results)
    })
})
// 新增

app.listen(3001,function(){
   console.log('服务启动')
})